package controller

import (
	"fmt"
	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/net/ghttp"
	"github.com/gogf/gf/v2/util/gconv"
	v1 "seawolf-admin-go/api/admin/v1"
	v2 "seawolf-admin-go/api/admin/v2"
	"seawolf-admin-go/internal/request/BaseReq"
	"seawolf-admin-go/internal/routers"
	"seawolf-admin-go/utility/response"
	"seawolf-admin-go/utility/token"
)

type cHello struct {
	*BaseController
}

func NewHelloController(group *ghttp.RouterGroup, defaultVersion string) {
	// 初始化内部变量
	inputReq := &BaseReq.I{}
	c := &cHello{&BaseController{Req: inputReq}}
	c.New(group, defaultVersion)
}

func (c *cHello) New(group *ghttp.RouterGroup, defaultVersion string) {
	// 配置表名
	c.Req = &BaseReq.I{
		TableName:          "admin",
		InfoIgnoreProperty: "password,password_salt,delete_at",
		SoftDeleteField:    "delete_at",
		ListQueryOp: &BaseReq.QueryOp{
			IsCache: 600,
		},
	}

	// 处理初始化方法和结束方法中间件
	group.Middleware(func(r *ghttp.Request) {
		c.Init(r)
		r.Middleware.Next()
		c.Shut(r)
	})
	// 注册基础路由
	RegBase(group, c, nil)

	group.GET("/test2", func(r *ghttp.Request) {
		r.Response.Writeln("Hello World!")
	})
	group.Map(map[string]interface{}{
		"get:/test3": func(r *ghttp.Request) {
			routers.MethodCallWithVersionStrategy(r, map[string]interface{}{
				"1.0.0":  c.Hello,
				"1.0.2":  c.HelloV2,
				"1.1.2":  c.HelloV2,
				"1.1.11": c.HelloV2,
				"1.0.1":  c.HelloV2,
			}, defaultVersion, routers.DemoteStrategy)
		},
		/*"get:/test4": func(r *ghttp.Request) {
			routers.MethodCallWithVersionStrategy(r, map[string]interface{}{
				"1.0.0": Hello.Init,
			}, defaultVersion, routers.DemoteStrategy)
		},*/
		//"post:/test4": controller.Hello2.Hello,
		"get:/test11": func(r *ghttp.Request) {
			routers.MethodCallWithVersionStrategy(r, map[string]interface{}{
				"1.0.0": c.Hello11,
			}, defaultVersion, routers.DemoteStrategy)
		},
		"get:/test12": func(r *ghttp.Request) {
			routers.MethodCallWithVersionStrategy(r, map[string]interface{}{
				"1.0.0": c.Hello12,
			}, defaultVersion, routers.DemoteStrategy)
		},
		"get:/test13": func(r *ghttp.Request) {
			routers.MethodCallWithVersionStrategy(r, map[string]interface{}{
				"1.0.0": c.Hello13,
			}, defaultVersion, routers.DemoteStrategy)
		},
		"get:/test14": func(r *ghttp.Request) {
			routers.MethodCallWithVersionStrategy(r, map[string]interface{}{
				"1.0.0": c.Hello14,
			}, defaultVersion, routers.DemoteStrategy)
		},
		"get:/test15": func(r *ghttp.Request) {
			routers.MethodCallWithVersionStrategy(r, map[string]interface{}{
				"1.0.0": c.Hello15,
			}, defaultVersion, routers.DemoteStrategy)
		},
		"get:/test16": func(r *ghttp.Request) {
			routers.MethodCallWithVersionStrategy(r, map[string]interface{}{
				"1.0.0": c.Hello16,
			}, defaultVersion, routers.DemoteStrategy)
		},
	})
}

func (c *cHello) Hello(r *ghttp.Request, req *v1.HelloReq) (res *v1.HelloRes, err error) {
	//g.RequestFromCtx(ctx).Response.Writeln("Hello World!")
	r.Response.WriteJson(response.SuccessByRequest(r))
	return
}

func (c *cHello) HelloV2(r *ghttp.Request, req *v2.HelloReq) (res *v2.HelloRes, err error) {
	//g.RequestFromCtx(ctx).Response.Writeln("Hello World!")
	res = &v2.HelloRes{
		C: "Hello World222222222!",
	}
	return
}

func (c *cHello) Hello11(r *ghttp.Request, req *v2.HelloReq) (res *v2.HelloRes, err error) {
	ctx := r.Context()
	loginToken, err := token.GenerateLoginToken(ctx, 22)
	if err != nil {
		return nil, err
	}
	res = &v2.HelloRes{
		C: loginToken,
	}
	return
}
func (c *cHello) Hello12(r *ghttp.Request, req *v2.HelloReq) (res *v2.HelloRes, err error) {
	ctx := r.Context()
	token.UpdateSessionData(ctx, 22, g.Map{"a": 1})
	res = &v2.HelloRes{
		C: "Hello World222222222!",
	}
	return
}
func (c *cHello) Hello13(r *ghttp.Request, req *v2.HelloReq) (res *v2.HelloRes, err error) {
	ctx := r.Context()
	fmt.Println(token.GetSessionData(ctx, 22))
	res = &v2.HelloRes{
		C: "Hello World222222222!",
	}
	return
}
func (c *cHello) Hello14(r *ghttp.Request, req *v2.HelloReq) (res *v2.HelloRes, err error) {
	ctx := r.Context()
	memberId, err := token.VerifyLoginToken(ctx, "0b5a553f26d9d8dea6f3e8c577424b25")
	if err != nil {
		return nil, err
	}
	fmt.Println(memberId)
	res = &v2.HelloRes{
		C: "Hello World222222222!",
	}
	return
}
func (c *cHello) Hello15(r *ghttp.Request, req *v2.HelloReq) (res *v2.HelloRes, err error) {
	ctx := r.Context()
	memberId := token.GetMustMemberIdByToken(ctx, "6926e58dd1d0ef3c096cf0d2e5e0aa63")
	status := token.GetSessionStatus(ctx, gconv.String(memberId))
	fmt.Println(status)
	res = &v2.HelloRes{
		C: "Hello World222222222!",
	}
	return
}
func (c *cHello) Hello16(r *ghttp.Request, req *v2.HelloReq) (res *v2.HelloRes, err error) {
	ctx := r.Context()
	tokenString := "0b5a553f26d9d8dea6f3e8c577424b25"
	fmt.Println(req.B)
	switch req.B {
	case "1":
		fmt.Println("kickout")
		token.KickoutByToken(ctx, tokenString)
		break
	case "2":
		fmt.Println("logout")
		token.LogoutByToken(ctx, tokenString)
		break
	}
	res = &v2.HelloRes{
		C: "Hello World222222222!",
	}
	return
}

/*func (c *cHello) List(r *ghttp.Request) *response.JsonResponse {
	fmt.Println("BaseController.List hello")
	return response.SuccessByRequest(r)
}*/
